package com.shop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.mapper.ProductInfoMapper;
import com.shop.pojo.ProductInfo;
import com.shop.pojo.ProductInfoExample;
import com.shop.pojo.vo.ProductInfoVo;
import com.shop.service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class ProductInfoServiceImpl implements ProductInfoService {

    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Override
    public List<ProductInfo> findAll() {

        return this.productInfoMapper.selectByExample(new ProductInfoExample());
    }

    @Override
    public PageInfo<ProductInfo> sliptPage(int nowPage, int pageSize) {
        PageHelper.startPage(nowPage,pageSize);
//        查询所有记录，插件的本质是使用了拦截器，在拼接sql后面使用 limt 0 5 来分页

        ProductInfoExample productInfoExample = new ProductInfoExample();
        productInfoExample.setOrderByClause("p_id desc");
        List<ProductInfo> list = this.productInfoMapper.selectByExample(productInfoExample);
        PageInfo<ProductInfo> productInfoPageInfo = new PageInfo<>(list);
        return productInfoPageInfo;
    }

    @Override
    public PageInfo<ProductInfo> sliptPage(ProductInfoVo productInfoVo,int pageSize) {
        PageHelper.startPage(productInfoVo.getPage(),pageSize);
//        查询所有记录，插件的本质是使用了拦截器，在拼接sql后面使用 limt 0 5 来分页
        List<ProductInfo> list = this.productInfoMapper.conditionSelect(productInfoVo);
        PageInfo<ProductInfo> productInfoPageInfo = new PageInfo<>(list);
        return productInfoPageInfo;
    }

    @Override
    public Integer addProduct(ProductInfo productInfo) {

        return this.productInfoMapper.insert(productInfo);
    }

    @Override
    public Integer removeProduct(Integer pid) {
        int count = -1;
//        如何订单下单了就无法删除报错，我们向上抛出
        try {
           count = this.productInfoMapper.deleteByPrimaryKey(pid);
        } catch (Exception e) {
            throw e;
        }


        return count;
    }

    @Override
    public Integer removeBatchProduct(String[] pids) {
        int count = this.productInfoMapper.deleteIds(pids);


        return count;
    }

    @Override
    public ProductInfo findById(Integer pId) {

        return this.productInfoMapper.selectByPrimaryKey(pId);
    }

    @Override
    public Integer modifyProduct(ProductInfo productInfo) {
        int count = this.productInfoMapper.updateByPrimaryKey(productInfo);

        return count;
    }

    @Override
    public List<ProductInfo> findCondition(ProductInfoVo productInfoVo) {
        List<ProductInfo> productInfos = this.productInfoMapper.conditionSelect(productInfoVo);



        return productInfos;
    }


}
